package ru.aslanov.schedule.client.util;

import com.smartgwt.client.data.*;
import com.smartgwt.client.util.BooleanCallback;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.EditorExitEvent;
import com.smartgwt.client.widgets.grid.events.EditorExitHandler;
import ru.aslanov.schedule.client.i18n.I18nUtil;

import java.util.Collection;
import java.util.Collections;

/**
 * Created by IntelliJ IDEA.
 * Created: Feb 11, 2010 5:26:20 PM
 *
 * @author Sergey Aslanov
 */
public class GUIUtil {
    private static final EditorExitHandler EDITOR_EXIT_HANDLER = new EditorExitHandler() {
        @Override
        public void onEditorExit(EditorExitEvent editorExitEvent) {
            if (editorExitEvent.getNewValue() == null) {
                editorExitEvent.getGrid().setEditValue(editorExitEvent.getRowNum(), editorExitEvent.getColNum(), (String) null);
            }
        }
    };

    public static void setupFKField(Collection<ListGridField> listGridFields, Collection<FormItem> formItems,
                                    String fieldName, String valueField,
                                    String displayField, String sortField,
                                    DataSource optionsDataSource, boolean allowEmptyValue, String listGridDisplayField) {
        final SelectItem selectItem = createSelect(fieldName, valueField, displayField, sortField, optionsDataSource, allowEmptyValue);

        if (listGridFields != null) {
            final ListGridField listGridField = new ListGridField(fieldName);
            //if (listGridDisplayField != null) {
            //    listGridField.setDisplayField(listGridDisplayField);
            //} else {
            listGridField.setOptionDataSource(optionsDataSource);
            listGridField.setValueField(valueField);
            listGridField.setDisplayField(displayField);
            //}
            listGridField.addEditorExitHandler(EDITOR_EXIT_HANDLER);

            listGridField.setEditorType(selectItem);

            listGridFields.add(listGridField);
        }

        if (formItems != null) {
            formItems.add(selectItem);
        }
    }

    public static SelectItem createSelect(String fieldName, String valueField, String displayField, String sortField, DataSource optionsDataSource, boolean allowEmptyValue) {
        final SelectItem selectItem = new SelectItem(fieldName);
        selectItem.setOptionDataSource(optionsDataSource);
        selectItem.setValueField(valueField);
        selectItem.setDisplayField(displayField);
        selectItem.setSortField(sortField);
        selectItem.setAllowEmptyValue(allowEmptyValue);
        return selectItem;
    }

    public static void addAddHandler(final ListGrid listGrid, com.smartgwt.client.widgets.Button button, final String scheduleKey) {
        button.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
            @Override
            public void onClick(com.smartgwt.client.widgets.events.ClickEvent clickEvent) {
                if (scheduleKey != null) {
                    listGrid.startEditingNew(Collections.singletonMap("schedule", scheduleKey));
                } else {
                    listGrid.startEditingNew();
                }
            }
        });
    }

    public static void addRemoveHandler(final ListGrid listGrid, com.smartgwt.client.widgets.Button button, final DynamicForm dynamicForm) {
        button.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
            @Override
            public void onClick(com.smartgwt.client.widgets.events.ClickEvent clickEvent) {
                if (listGrid.getSelectedRecord() != null) {
                    SC.confirm(I18nUtil.cnt().areYouSure(), new BooleanCallback() {
                        @Override
                        public void execute(Boolean aBoolean) {
                            if (aBoolean) {
                                listGrid.removeSelectedData();
                                if (dynamicForm != null) dynamicForm.clear();
                            }
                        }
                    });
                }
            }
        });
    }

    public static void addDataSynchroniously(final ListGrid listGrid, final Record[] records) {
        addDataSynchroniously(listGrid, records, 0);
    }
    private static void addDataSynchroniously(final ListGrid listGrid, final Record[] records, final int curr) {
        listGrid.addData(records[curr], new DSCallback() {
            @Override
            public void execute(DSResponse dsResponse, Object o, DSRequest dsRequest) {
                if (curr < records.length - 1) {
                    updateDataSynchroniously(listGrid, records, curr+1);
                }
            }
        });
    }

    public static void updateDataSynchroniously(final ListGrid listGrid, final Record[] records) {
        updateDataSynchroniously(listGrid, records, 0);                           
    }
    private static void updateDataSynchroniously(final ListGrid listGrid, final Record[] records, final int curr) {
        listGrid.updateData(records[curr], new DSCallback() {
            @Override
            public void execute(DSResponse dsResponse, Object o, DSRequest dsRequest) {
                if (curr < records.length - 1) {
                    updateDataSynchroniously(listGrid, records, curr+1);
                }
            }
        });
    }
}
